package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.entry.Address;
import com.server.AddressService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class RegisterController {
    @Resource
    AddressService addressService;

    /*@RequestMapping(value = "/address", method = RequestMethod.GET)
    public String goToPage(Map<String,Object> map) {
        String addressId = "110000";//默认
        String cityAddressId = "110100";//默认
        System.out.println("获取省份信息");
        List<Address> addressList = addressService.getRoot();
        System.out.println("获取addressId为{}的市级地址信息"+addressId);
        List<Address> cityAddress = addressService.getList(null, addressId);
        System.out.println("获取cityAddressId为{}的区级地址信息"+cityAddressId);
        List<Address> districtAddress = addressService.getList(null, cityAddressId);
        map.put("addressList", addressList);
        map.put("cityList", cityAddress);
        map.put("districtList", districtAddress);
        map.put("addressId", addressId);
        map.put("cityAddressId", cityAddressId);
        System.out.println("转到前台-用户注册页");
        return "register";
    }*/


    //加载地址信息
    /**
     * 根据address_areaId获取地址信息-ajax
     * @param areaId 省id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/address", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    protected String getAddressByAreaId(@Param("areaId") String areaId) {
        JSONObject object = new JSONObject();
        System.out.println("获取AreaId为{}的地址信息="+areaId);
        //获取省下的市集合
        List<Address> addressList = addressService.getList(null, areaId);
        if (addressList == null || addressList.size() <= 0) {
            object.put("success", false);
            return object.toJSONString();
        }
        System.out.println("获取该地址可能的子地址信息");
        //获取第一个市下的区集合
        List<Address> childAddressList = addressService.getList(null, addressList.get(0).getAddress_areaId());
        object.put("success", true);
        object.put("addressList", addressList);
        object.put("childAddressList", childAddressList);
        return object.toJSONString();
    }
}
